package com.asj.vivo.lock.service.command.specific;

import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_BATTER_PERCENTAGE;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_INFO;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_PHONE_NUMBER;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_POSITION;
import static com.asj.enums.DeviceIdentifierDictEnums.DEVICE_WIFI_OR_FLOW;
import static com.asj.enums.DeviceIdentifierDictEnums.syncDeviceInfo;
import static com.asj.enums.HttpRequestDictEnums.receiveUpdateDeviceInfoResultUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.result_success;

import android.content.Context;
import android.util.Log;

import com.asj.service.binding.BindingGetDeviceInfoService;
import com.asj.service.oppoSdk.OPPOSDKService;
import com.asj.utils.CommonsUtils;
import com.asj.utils.HttpRequestUtils;
import com.asj.utils.JSONParamPackingUtils;
import com.google.gson.JsonObject;

/**
 * 同步设备信息
 * */
public class SpecificSyncDeviceInfoService {
    public static void syncDeviceInfo(JsonObject contentData, Context context){
        try {
            OPPOSDKService opposdkService = new OPPOSDKService(context);

            String syncDevice = contentData.get(syncDeviceInfo.getValue()).getAsString();
            JsonObject resultData = new JsonObject();
            //获取所有设备信息
            switch (syncDevice) {
                case "1":
                    resultData.addProperty(DEVICE_INFO.getValue(), opposdkService.getDeviceInfo());
                    resultData.addProperty(DEVICE_PHONE_NUMBER.getValue(), opposdkService.PhoneNumber());
                    resultData.addProperty(DEVICE_POSITION.getValue(), opposdkService.getDevicePosition());
                    resultData.addProperty(DEVICE_BATTER_PERCENTAGE.getValue(), BindingGetDeviceInfoService.getInstance(context).getBatteryPercentage());
                    resultData.addProperty(DEVICE_WIFI_OR_FLOW.getValue(), BindingGetDeviceInfoService.getInstance(context).getWifiOrFlow());
                    break;
                //获取电话信息
                case "2":
                    resultData.addProperty(DEVICE_PHONE_NUMBER.getValue(), opposdkService.PhoneNumber());
                    break;
                //获取位置信息
                case "3":
                    resultData.addProperty(DEVICE_POSITION.getValue(), opposdkService.getDevicePosition());
                    break;
            }
            JSONParamPackingUtils.functionPacking(contentData,result_success,null,resultData.toString(),CommonsUtils.getDeviceCode(opposdkService.getIMEI()));
        }catch (Exception e){
            JSONParamPackingUtils.functionPacking(contentData,result_success,e.getMessage());
        }
        new Thread(()->{
            //将请求发送回去
            String result = HttpRequestUtils.retryHttpRequest(receiveUpdateDeviceInfoResultUrl.getValue(), contentData.toString(), result_success.getCode(), 2,context);
            if(!result.equals(request_error.getMessage())){
                Log.w("同步设备信息已返回结果，告知成功！",result);
            }
        }).start();
    }
}
